package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reggie.common.R;
import com.reggie.entity.User;
import com.reggie.service.UserService;
import com.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 发送邮箱验证码
     *
     * @param user
     * @return
     */
    @PostMapping("/sendmsg")
    public R<String> sendMsg(@RequestBody User user, HttpServletRequest request) {
        // 获取邮箱账号
        String phone = user.getPhone();

        String subject = "周周花店登录验证码";//邮件标题

        if (StringUtils.isNotEmpty(phone)) {
            String code = ValidateCodeUtils.generateValidateCode(4).toString();//获取验证码
            String context = "欢迎使用周周花店，登录验证码为: " + code + ",五分钟内有效，请妥善保管!";
            //邮件文本
            log.info("code={}", code);

            // 真正地发送邮箱验证码
            userService.sendMsg(phone, subject, context);

            //  将随机生成的验证码保存到session中
            //            session.setAttribute(phone, code);

            // 验证码由保存到session 优化为 缓存到Redis中，并且设置验证码的有效时间为 5分钟
            request.getSession().setAttribute("code",code);
            //redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.MINUTES);

            return R.success("验证码发送成功，请及时查看!");
        }
        return R.error("验证码发送失败，请重新输入!");
    }

    /**
     * 移动端用户登录
     */
    @PostMapping("/login")
    public R<User> userLogin(HttpSession session,@RequestBody Map map){
        /**如果不点获取验证码 那么我们就给验证码整个默认值*/
        session.setAttribute("code","6666");

        log.info(map.toString());
        String phone = (String)map.get("phone");
        String code = (String)map.get("code");

        //先判断一这个账户 在user数据库表里面是否存在
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getPhone,phone);
        User user = userService.getOne(lqw);
        if(user == null){
            //没这个账户
            user = new User();
            user.setPhone(phone);
            userService.save(user);
        }
        //有账户了
        String codeReal = (String)session.getAttribute("code");
        //比对验证码
        if(!code.equals(codeReal)){
            //不相等的时候
            return R.error("登录失败,验证码错误!");
        }
        if(user.getStatus() == 0){
            return R.error("登录失败,用户已被禁用!");
        }
        //验证码相同
        session.setAttribute("user",user.getId());
        return R.success(user);
    }

    /**
     * 移动端用户退出
     */
    @PostMapping("loginout")
    public R<String> loginout(HttpSession session){
        //清理sessionid
        session.removeAttribute("user");
        return R.success("用户退出成功");
    }
}
